home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Games / Arashi 1.1.1 / source code / Game Source / jam src / GameLoop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-31  |  8.0 KB  |  370 lines  |  [TEXT/KAHL]

  1. /*/
  2.      Project Arashi: GameLoop.c
  3.      Major release: Version 1.1d2, 9/5/95
  4.  
  5.      Last modification: Thursday, August 31, 1995, 1:28
  6.      Created: Sunday, February 5, 1989, 2:21
  7.  
  8.      Copyright © 1989-1995, Juri Munkki
  9. /*/
  10.  
  11. #include "VA.h"
  12. #include "STORM.h"
  13. #include "Shuddup.h"
  14. #include "PlayOptions.h"
  15. #include "GamePause.h"
  16. #include "HighScoresToBeat.h"        /* mz */
  17. #include <GestaltEqu.h>
  18. #include <OSUtils.h>
  19. #include <Types.h>
  20. #include "heroflags.h"
  21. #include "LowMem.h"
  22.  
  23. /* #define DEBUGGING_ROUTINES */
  24. #define    UNNATURALDEATH    0
  25. #define NATURALDEATH    1
  26.  
  27. static     int    highestLastLevel=9;            /* (mz) */
  28.         int    FinalDeathMode=NATURALDEATH;
  29.         
  30. extern    Player    Hero;
  31. extern    int        OldSound;                /*    Flag for Sound Kit        */
  32.  
  33.  
  34. Initials    *HiScoresToBeat;
  35. int         ScoretoBeatIndex;
  36.  
  37. EventRecord        Event;
  38. WindowPtr        BackdropWind;
  39.  
  40.  
  41. void    GameLoop(Options)
  42. int        Options;
  43. {
  44.     int            i;
  45.     long int    levelStBonus=0;
  46.     long int    levelBonus=0;
  47.     int            levelComplete=0;
  48.     Handle        ScoreHand;
  49.     int            DoomsdayDeath = 0;
  50.     
  51.     /* read scores from resources  for top right corner */
  52.     if (PlayOptions->restart == 0)
  53.         ScoreHand=GetResource('SCOR',128);
  54.     else 
  55.         ScoreHand=GetResource('SCOR',129);
  56.     HLock(ScoreHand);
  57.     HiScoresToBeat = (Initials *)*ScoreHand;
  58.     ScoretoBeatIndex = 50;        /* start at the bottom */
  59.     
  60.     VAEraseBuffer();
  61.  
  62.     /*    Flush out some events and let MultiFinder update windows.    */
  63.     for(i=0;i<10;i++)
  64.         GameEvent();
  65.  
  66.     ZeroScore();
  67.     
  68.     if(Options == NormalPlay)
  69.     {    StartNormalPlay();
  70.     }
  71.     else
  72.     {    Hero.lives = 0;
  73.         if(Options == PlaybackPlay)
  74.             ThisLevel.lvNext = TickCount() & 7;
  75.  
  76.         LoadPlayRecord();
  77.         if(Options == RecordPlay)
  78.         {    
  79.             StartPlayRecord();
  80.         }
  81.         else
  82.         if(Options == PlaybackPlay)
  83.         {    
  84.             StartPlayback();
  85.         }
  86.     }
  87.  
  88.     DisplayLives();
  89.  
  90.     do
  91.     {    VACatchUp();
  92.  
  93.         DisplayScore();
  94.  
  95.         if(Hero.state != HeroDead)
  96.         {    AddLife();
  97.             if (levelComplete == 1)
  98.             {
  99.                 /* check to see if this was first level.  If so give STBonus.        */
  100.                 if (!(Hero.Flags & STBonusGivenMask)){
  101.                     
  102.                     /* set bonus granted and show zap flags                         */
  103.                     
  104.                     Hero.Flags |= (ShowSuperZapMsgMask + STBonusGivenMask ); 
  105.                     IncreaseScore(levelStBonus); /* give starting bonus                */
  106.                 } 
  107.                 
  108.                 Hero.Flags |= (ShowSuperZapMsgMask);
  109.                 
  110.                 /* if  a saved game and start level was already done then turn on    */
  111.                 /* superzap msg flag */
  112.                 
  113.                 IncreaseScore(levelBonus);
  114.                 DisplayScore();            
  115.             }
  116.             else
  117.                 if(!(Hero.Flags & SavedgameFirstLVLCompleteMask))
  118.                     Hero.Flags &= (FLAGSMASK - ShowSuperZapMsgMask);
  119.             levelComplete = 0;
  120.             levelBonus = 0;
  121.             levelStBonus = 0;
  122.             STLoadLevel(); 
  123.             CreateFit();
  124.         }
  125.         else if(DoomsdayDeath)
  126.         {
  127.             int    numzaps=ThisLevel.plSuperZaps;
  128.             int    lvl =     ThisLevel.lvNumber;
  129.             
  130.             STLoadLevel(); 
  131.             ThisLevel.plSuperZaps = numzaps;    /* Keep old # of zaps                 */
  132.             ThisLevel.lvNumber = lvl; 
  133.             ThisLevel.lvNext = lvl + 1;
  134.             Hero.Flags &= (FLAGSMASK - ShowSuperZapMsgMask); /* unset re-zap msg    */
  135.             CreateFit();
  136.             Hero.Flags |= ShowSuperZapMsgMask;    /* set show re-zap msg                 */
  137.             DoomsdayDeath = 0;
  138.             
  139.         }
  140.         else
  141.         {    VA.color=0;
  142.             RedrawField();
  143.         }
  144.         
  145.         RemoveLife();
  146.         STInitialize();
  147.     
  148.         do
  149.         {    if(Options != PlaybackPlay)
  150.             {    GameEvent();
  151.                 switch(Event.what)
  152.                 {    case keyDown:
  153.                     case autoKey:
  154.                         switch(Event.message & 0xFF)
  155.                         {    case 'q':
  156.                             case 'Q':
  157.                             case 27:
  158.                             case 13:    Hero.state=HeroDead;
  159.                                         Hero.lives=0;                
  160.                                         FinalDeathMode=UNNATURALDEATH;
  161.                                         break;
  162.                             case ' ':    /* Handled in player routine    */
  163.                                                                     break;
  164.                             case 'p':
  165.                             case 'P':    GamePause(GameRunning);     
  166.                                         break;
  167.     #ifdef DEBUGGING_ROUTINES
  168.                             case 26:    DumpThisLevel();            break;
  169.                             case '1':    AddLife();                    break;
  170.                             case '2':    RemoveLife();                break;
  171.  
  172.                             case 'z':    CreateNewFlipper();            break;
  173.                             case 'x':    CreateNewTanker(0);            break;
  174.                             case 's':    CreateNewTanker(1);            break;
  175.                             case 'd':    CreateNewTanker(2);            break;
  176.                             case 'c':    CreateNewPulsar();            break;
  177.                             case 'v':    CreateNewBall();            break;
  178.                             case 'o':    ThisLevel.totalCount = 0;    break;
  179.  
  180.                         /*    case '@':    Hero.state=HeroFlying;
  181.                                         DemoMode();                    break;
  182.                             case 'k':    TestZoomRect();                break;
  183.                             case 'l':    DoLevelSelect();            break; */
  184.                             default:    Hero.state=HeroFlying;        break;
  185.      #endif
  186.                         }
  187.                         break;
  188.                 }
  189.             }
  190.             else    /*    Playback active.    */
  191.             {    if(GamePeekEvent())
  192.                 {    Hero.lives = 0;
  193.                     Hero.state = HeroDead;
  194.                 }
  195.                 Event.what = 0;
  196.             }
  197.             if(ThisLevel.activeCount<ThisLevel.boredomCount)
  198.                 ThisLevel.probIncrease=ThisLevel.boredProb;
  199.             else
  200.                 ThisLevel.probIncrease=0;
  201.             ThisLevel.activeCount=0;
  202.  
  203.             STUpdate();
  204.  
  205.             VA.segmscale=5;
  206.  
  207.             VA.color = BG2; 
  208.             if (!(PlayOptions->restart == 0))    /* only show count if Practice mode */
  209.             {
  210.                 if (ThisLevel.lvColor == 5)        /* check for invis lvl where BG2 is */
  211.                     VA.color=BG1;                /* also invis. (mz)                 */
  212.                 VADrawPadNumber(ThisLevel.totalCount,VA.frame.right-10,60,3);
  213.                 VADrawPadNumber(ThisLevel.edgeCount,VA.frame.right-10,80,3);
  214.                 VA.color = BG2;
  215.             }
  216.             VAStep();
  217.             
  218.             if(Hero.state == HeroPlaying)
  219.             {    if(ThisLevel.edgeCount==ThisLevel.totalCount)
  220.                 {    Hero.endtimer--;
  221.                     if(Hero.endtimer<=0)
  222.                     {    Hero.state=HeroFlying;
  223.                         Hero.flydepth=0;
  224.                         levelComplete=1;
  225.                         levelStBonus=ThisLevel.lvStBonus;
  226.                         levelBonus=ThisLevel.lvBonus;
  227.                     }
  228.                 }
  229.             }
  230.         
  231.         } while(Hero.state != HeroDead && Hero.state != HeroFlying);
  232.  
  233.         if(Hero.state == HeroFlying)
  234.             STFlyThruLoop(Options);
  235.             
  236.         if( (ThisLevel.Doomsday==1) && (Hero.lives > 0)) /* player has died in Doomsday level */
  237.         {    DoomsdayDeath = 1;
  238.             VA.color=-1;    /*    Black                    */
  239.             RedrawField();
  240.             RedrawSpikes();
  241.         }
  242.     
  243.     } while(Hero.lives>0 || Hero.state != HeroDead);
  244.     if(FinalDeathMode  && (Options == NormalPlay))
  245.         STFlyOutLoop();                                /* Fly out at end */
  246.     
  247.     FinalDeathMode = NATURALDEATH;
  248.  
  249.     if(Options != PlaybackPlay)
  250.         highestLastLevel = ThisLevel.lvNumber - 1;    /* save highest level *completed*    */
  251.     
  252.     /* Make sure saved game flag does not carry into next game.            
  253.     Hero.Flags &= (FLAGSMASK - SavedGameStartMask); */
  254.     
  255.     Hero.Flags = NOFLAGSMASK;
  256.     
  257.     if(Options != NormalPlay)
  258.     {    if(Options == RecordPlay)
  259.         {    WriteRecordedPlay();
  260.         }
  261.         UnloadPlayRecord();
  262.     }    
  263. }
  264.  
  265.  
  266. int        StormStart()
  267. {
  268.     int                zero=0;
  269.     int                i;
  270.     Handle            TheDevice;
  271.     long            myFeature;
  272.     
  273.     DoInits();
  274. //    StripAllPictComments();
  275.     
  276.     if(installASHI())
  277.         return -1;
  278.         
  279.     DisableSuperClock(); 
  280.     
  281.     /* Now do check for Color Quickdraw. Any version above simple 8 bit will do. */
  282.     if(Gestalt(gestaltQuickdrawVersion, &myFeature))
  283.     {
  284.         ParamText("\pARASHI encountered an unrecoverable _GESTALT error.",nil,nil,nil);
  285.         Alert(131,0);
  286.         ExitToShell();
  287.     }
  288.     if (myFeature < gestalt8BitQD)
  289.     {
  290.         ParamText("\pARASHI requires Color Quickdraw and has not detected it in your System.",nil,nil,nil);
  291.         Alert(131,0);
  292.         ExitToShell();
  293.     }
  294.     
  295.     
  296.     randSeed= TickCount();
  297.     VARandSeed= TickCount();
  298.  
  299.     TheDevice=ScreenSelect();
  300.     if(TheDevice==0)    return -1;
  301.     
  302.     if(PlayOptions->sys607Sound)
  303.     {    OldSound=0;
  304.     }
  305.     else
  306.     {    OldSound=1;
  307.     }
  308.     if(!PlayOptions->soundOff)
  309.         InitSoundKit();
  310.         
  311.     if(PlayOptions->blankUnused)
  312.         BackdropWind = NewWindow(0,&((*LMGetGrayRgn())->rgnBBox),&zero,-1,plainDBox,0,0,0);
  313.     else
  314.     {    Rect    SmallRect = { 0,0,10,10};
  315.         BackdropWind = NewWindow(0,&SmallRect,&zero,-1,plainDBox,0,0,0);
  316.     }    
  317.     VAInit(TheDevice);
  318.                     
  319.     if(PlayOptions->verticalGame)
  320.     {    i=VA.frame.bottom*4L/5;
  321.         if(VA.frame.right>i)
  322.             VA.frame.right=i;
  323.     }
  324.  
  325.     VAInitFractalLines();
  326.     STAllocate();
  327.  
  328.     /*    Flush out some events and let MultiFinder update windows.    */
  329.     for(i=0;i<10;i++)    GameEvent();
  330.     
  331.     return 0;
  332. }
  333.  
  334. long    PlayGame(HighScore,Options)
  335. long    HighScore;
  336. int        Options;
  337. {
  338.     int        i;
  339.     
  340.     Hero.score = -1;
  341.     i=DoLevelSelect(highestLastLevel);
  342.     if(Hero.Flags & SavedGameStartMask)
  343.         Hero.Flags &= (FLAGSMASK - SavedgameFirstLVLCompleteMask);
  344.     if(i>=0)
  345.     {    VA.FrameSpeed=3;
  346.         ThisLevel.lvNext=i;
  347.         GameLoop(Options);
  348.     }
  349.     else
  350.         Hero.score = -1;    
  351.     return    Hero.score;
  352. }
  353.  
  354. void    PlayDemoGame()
  355. {
  356.     long    seeder;
  357.     
  358.     seeder = VARandSeed;
  359.     GameLoop(PlaybackPlay);
  360.     VARandSeed = seeder;
  361. }
  362.  
  363. void    RecordDemoGame()
  364. {
  365.     static    demoLevelStart = 10;
  366.  
  367.     ThisLevel.lvNext = demoLevelStart;
  368.     demoLevelStart+=2;
  369.     GameLoop(RecordPlay);
  370. }